"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var crypto_js_1 = tslib_1.__importDefault(require("crypto-js"));
var KEY = 'J90a*&j_~!ks^T4j';
var IV = '';
var AESUtils = /** @class */ (function () {
    function AESUtils() {
    }
    // constructor(){}
    /**
     * AES解密
     * @param text
     * @param option AES解密所需配置属性
     * @param option.key AES解密密钥
     * @param option.iv AES解密密钥偏移量
     * @param option.mode AES解密模式
     * @param option.pad AES解密填充
     * @returns string解密明文
     */
    AESUtils.decrypt = function (text, option) {
        var decryptOption = AESUtils.formatAESOption(option);
        var encryptedHexStr = crypto_js_1.default.enc.Hex.parse(text);
        var srcs = crypto_js_1.default.enc.Base64.stringify(encryptedHexStr);
        //
        var decrypt = crypto_js_1.default.AES.decrypt(srcs, decryptOption.key, decryptOption);
        var decryptedStr = decrypt.toString(crypto_js_1.default.enc.Utf8);
        return decryptedStr.toString();
    };
    /**
     * AES加密
     * @param word 需要加密字符串
     * @param option AES加密所需配置属性
     * @param option.key AES加密密钥
     * @param option.iv AES加密密钥偏移量
     * @param option.mode AES加密模式
     * @param option.pad AES加密填充
     * @returns string密文
     */
    AESUtils.encrypt = function (word, option) {
        var encryptOption = AESUtils.formatAESOption(option);
        var srcs = crypto_js_1.default.enc.Utf8.parse(word);
        var encrypted = crypto_js_1.default.AES.encrypt(srcs, encryptOption.key, encryptOption);
        return encrypted.ciphertext.toString().toUpperCase();
    };
    /**
     * AES加密解密option参数整理
     * @param option AES加密所需配置属性
     * @param option.key AES加密密钥
     * @param option.iv AES加密密钥偏移量
     * @param option.mode AES加密模式
     * @param option.pad AES加密填充
     * @returns CipherOption
     */
    AESUtils.formatAESOption = function (option) {
        if (option === void 0) { option = {}; }
        var key = crypto_js_1.default.enc.Utf8.parse(option.key || KEY);
        //
        var iv = crypto_js_1.default.enc.Utf8.parse(option.iv || IV);
        var mode = crypto_js_1.default.mode[option.mode || 'CBC'];
        var pad = crypto_js_1.default.pad[option.pad || 'Pkcs7'];
        return {
            key: key,
            //
            iv: iv,
            mode: mode,
            pad: pad
        };
    };
    return AESUtils;
}());
exports.default = AESUtils;
